import {
  Component,
  Input,
  Output,
  EventEmitter,
  OnInit,
  OnChanges
} from "@angular/core";
import {
  NzNotificationService,
  NzModalSubject,
  NzModalService,
  UploadFile
} from "ng-zorro-antd";
import { HttpServcie } from "../../../../service/http.service";
import { Config } from "../../../../service/config";
import { FormBuilder, FormGroup, Validators } from "@angular/forms";
import { Observable } from "rxjs/RX";
import { PlanDictBean } from "../plan-dict.bean";
import * as moment from "moment";

@Component({
  selector: "check-item",
  templateUrl: "check-item.html",
  styleUrls: ["check-item.scss"]
})
export class CheckItemPage implements OnInit, OnChanges {
  validateForm: FormGroup;
  isSpinning: boolean = false;
  @Output("outPut")
  outPut = new EventEmitter();
  @Input()
  inValue: any = [];
  config: any = Config;
  //系统类型
  planDictBean: PlanDictBean = new PlanDictBean();
  roleTypeList = [];
  _useroptions = []; //角色类型
  _cardTypeoption = [];
  partyList = [];
  user: any = {};
  planDictId: string;
  total: string;
  currentModal: any;
  productList: any = [];
  productallChecked = false;
  productindeterminate = false;

  usedList: any = [];
  usedallChecked = false;
  usedindeterminate = false;

  elevatorList: any = [];
  elevatorallChecked = false;
  elevatorindeterminate = false;

  checkedList1: any = [];
  checkedallChecked1 = false;
  checkedindeterminate1 = false;

  checkedList2: any = [];
  checkedallChecked2 = false;
  checkedindeterminate2 = false;

  checkedList3: any = [];
  checkedallChecked3 = false;
  checkedindeterminate3 = false;

  checkedList4: any = [];
  checkedallChecked4 = false;
  checkedindeterminate4 = false;

  selfList: any = [];
  selfallChecked = false;
  selfindeterminate = false;
  inCheckItemData: any = {};
  deleteData: any = {};

  selected: Array<any> = [];
  constructor(
    private fb: FormBuilder,
    private httpServcie: HttpServcie,
    private _notification: NzNotificationService,
    private nsub: NzModalSubject,
    private modalService: NzModalService
  ) { }

  ngOnInit() {
    this.validateForm = this.fb.group({
      pdName: [null, [Validators.required]], //用户姓名
      pdType: [null, [Validators.required]], //用户持证类型
      pPartyName: [null, [Validators.required]], //证件号
      pUserName: [null, [Validators.required]], //身份证号码
      pStartDt: [null, [Validators.required]], //单位全称
      pEndDt: [null, [Validators.nullValidator]] //办公电话
    });

    this.user = JSON.parse(window.localStorage.getItem("user"));
    this.planDictBean.pPartyName = this.user.partyName;
    this.planDictBean.pPartyId = this.user.partyId;
    this.planDictBean.pUserId = this.user.id;
  }

  ngOnChanges() {
    this.planDictId = this.inValue.id;
    this.inCheckItemData = { pDictId: this.planDictId };
    this.httpServcie
      .queryPlanDictItem({ pDictId: this.planDictId })
      .subscribe(data => {
        let unmakedList = data.data.list;
        if (unmakedList) {
          unmakedList.forEach(element => {
            element.itemContents.forEach(item => {
              this.selected.push(item);
            })
          });
        }
        console.log(this.selected);

        let params = { itemType: "1" };
        this.httpServcie.queryCheckItem(params).subscribe(data => {
          if (data.status == 200) {
            this.productList = data.data.list;
            this.productList.forEach(element => {
              this.selected.forEach(element1 => {
                if (element1.id == element.id) {
                  element.checked = true;
                }
              });
            });
          }
        });

        let params1 = { itemType: "2" };
        this.httpServcie.queryCheckItem(params1).subscribe(data => {
          if (data.status == 200) {
            this.usedList = data.data.list;
            this.usedList.forEach(element => {
              this.selected.forEach(element1 => {
                if (element1.id == element.id) {
                  element.checked = true;
                }
              });
            });
          }
        });

        let params2 = { itemType: "3" };
        this.httpServcie.queryCheckItem(params2).subscribe(data => {
          if (data.status == 200) {
            this.elevatorList = data.data.list;
            this.elevatorList.forEach(element => {
              this.selected.forEach(element1 => {
                if (element1.id == element.id) {
                  element.checked = true;
                }
              });
            });
          }
        });

        let params3 = { itemType: "4", adDictId: this.planDictId };
        this.httpServcie.queryCheckItem(params3).subscribe(data => {
          if (data.status == 200) {
            this.selfList = data.data.list;
            this.selfList.forEach(element => {
              this.selected.forEach(element1 => {
                if (element1.id == element.id) {
                  element.checked = true;
                }
              });
            });
          }
        });

        let params41 = { itemType: "5", itemName: "曳引与强制驱动电梯" };
        this.httpServcie.queryCheckItem(params41).subscribe(data => {
          if (data.status == 200) {
            this.checkedList1 = data.data.list;
            this.checkedList1.forEach(element => {
              this.selected.forEach(element1 => {
                if (element1.id == element.id) {
                  element.checked = true;
                }
              });
            });
          }
        });

        let params42 = { itemType: "5", itemName: "自动扶梯与自动人行道" };
        this.httpServcie.queryCheckItem(params42).subscribe(data => {
          if (data.status == 200) {
            this.checkedList2 = data.data.list;
            this.checkedList2.forEach(element => {
              this.selected.forEach(element1 => {
                if (element1.id == element.id) {
                  element.checked = true;
                }
              });
            });
          }
        });

        let params43 = { itemType: "5", itemName: "液压电梯" };
        this.httpServcie.queryCheckItem(params43).subscribe(data => {
          if (data.status == 200) {
            this.checkedList3 = data.data.list;
            this.checkedList3.forEach(element => {
              this.selected.forEach(element1 => {
                if (element1.id == element.id) {
                  element.checked = true;
                }
              });
            });
          }
        });

        let params44 = { itemType: "5", itemName: "杂物电梯" };
        this.httpServcie.queryCheckItem(params44).subscribe(data => {
          if (data.status == 200) {
            this.checkedList4 = data.data.list;
            this.checkedList4.forEach(element => {
              this.selected.forEach(element1 => {
                if (element1.id == element.id) {
                  element.checked = true;
                }
              });
            });
          }
        });
      });
  }

  submitForm($event) {
    const updateList: any = [];
    this.productList.forEach(element => {
      if (element.checked) {
        updateList.push({ pDictId: this.planDictId, itemId: element.id });
      }
    });

    this.usedList.forEach(element => {
      if (element.checked) {
        updateList.push({ pDictId: this.planDictId, itemId: element.id });
      }
    });

    this.elevatorList.forEach(element => {
      if (element.checked) {
        updateList.push({ pDictId: this.planDictId, itemId: element.id });
      }
    });

    this.selfList.forEach(element => {
      if (element.checked) {
        updateList.push({ pDictId: this.planDictId, itemId: element.id });
      }
    });

    this.checkedList1.forEach(element => {
      if (element.checked) {
        updateList.push({ pDictId: this.planDictId, itemId: element.id });
      }
    });

    this.checkedList2.forEach(element => {
      if (element.checked) {
        updateList.push({ pDictId: this.planDictId, itemId: element.id });
      }
    });

    this.checkedList3.forEach(element => {
      if (element.checked) {
        updateList.push({ pDictId: this.planDictId, itemId: element.id });
      }
    });

    this.checkedList4.forEach(element => {
      if (element.checked) {
        updateList.push({ pDictId: this.planDictId, itemId: element.id });
      }
    });

    let ob = this.httpServcie.addPlanDictItem({ pdiList: updateList });
    ob.subscribe(data => {
      this.isSpinning = false;
      if (data.status == 200) {
        this._notification.create("success", "成功提示", "添加成功");
        this.outPut.emit($event);
      } else {
        this._notification.create("error", "失败提示", data.message);
        return;
      }
    });
  }

  //交验
  getFormControl(name) {
    return this.validateForm.controls[name];
  }

  //table加载数据时
  productdisplayDataChange($event) {
    this.productList = $event;
    this.productrefreshStatus();
  }

  //重置选择框
  productrefreshStatus() {
    const allChecked = this.productList.every(
      value => value.disabled || value.checked
    );
    const allUnChecked = this.productList.every(
      value => value.disabled || !value.checked
    );
    this.productallChecked = allChecked;
    this.productindeterminate = !allChecked && !allUnChecked;
  }

  //全选
  productcheckAll(value) {
    if (value) {
      this.productList.forEach(data => {
        if (!data.disabled) {
          data.checked = true;
        }
      });
    } else {
      this.productList.forEach(data => (data.checked = false));
    }
  }

  //table加载数据时
  useddisplayDataChange($event) {
    this.usedList = $event;
    this.usedrefreshStatus();
  }

  //重置选择框
  usedrefreshStatus() {
    const allChecked = this.usedList.every(
      value => value.disabled || value.checked
    );
    const allUnChecked = this.usedList.every(
      value => value.disabled || !value.checked
    );
    this.usedallChecked = allChecked;
    this.usedindeterminate = !allChecked && !allUnChecked;
  }

  //全选
  usedcheckAll(value) {
    if (value) {
      this.usedList.forEach(data => {
        if (!data.disabled) {
          data.checked = true;
        }
      });
    } else {
      this.usedList.forEach(data => (data.checked = false));
    }
  }

  //table加载数据时
  elevatordisplayDataChange($event) {
    this.elevatorList = $event;
    this.elevatorrefreshStatus();
  }

  //重置选择框
  elevatorrefreshStatus() {
    const allChecked = this.elevatorList.every(
      value => value.disabled || value.checked
    );
    const allUnChecked = this.elevatorList.every(
      value => value.disabled || !value.checked
    );
    this.elevatorallChecked = allChecked;
    this.elevatorindeterminate = !allChecked && !allUnChecked;
  }

  //全选
  elevatorcheckAll(value) {
    if (value) {
      this.elevatorList.forEach(data => {
        if (!data.disabled) {
          data.checked = true;
        }
      });
    } else {
      this.elevatorList.forEach(data => (data.checked = false));
    }
  }

  //table加载数据时
  selfdisplayDataChange($event) {
    this.selfList = $event;
    this.selfrefreshStatus();
  }

  //重置选择框
  selfrefreshStatus() {
    const allChecked = this.selfList.every(
      value => value.disabled || value.checked
    );
    const allUnChecked = this.selfList.every(
      value => value.disabled || !value.checked
    );
    this.selfallChecked = allChecked;
    this.selfindeterminate = !allChecked && !allUnChecked;
  }

  //全选
  selfcheckAll(value) {
    if (value) {
      this.selfList.forEach(data => {
        if (!data.disabled) {
          data.checked = true;
        }
      });
    } else {
      this.selfList.forEach(data => (data.checked = false));
    }
  }

  //table加载数据时
  checkeddisplayDataChange1($event) {
    this.checkedList1 = $event;
    this.checkedrefreshStatus1();
  }

  //重置选择框
  checkedrefreshStatus1() {
    const allChecked = this.checkedList1.every(
      value => value.disabled || value.checked
    );
    const allUnChecked = this.checkedList1.every(
      value => value.disabled || !value.checked
    );
    this.checkedallChecked1 = allChecked;
    this.checkedindeterminate1 = !allChecked && !allUnChecked;
  }

  //全选
  checkedcheckAll1(value) {
    if (value) {
      this.checkedList1.forEach(data => {
        if (!data.disabled) {
          data.checked = true;
        }
      });
    } else {
      this.checkedList1.forEach(data => (data.checked = false));
    }
  }

  //table加载数据时
  checkeddisplayDataChange2($event) {
    this.checkedList2 = $event;
    this.checkedrefreshStatus2();
  }

  //重置选择框
  checkedrefreshStatus2() {
    const allChecked = this.checkedList2.every(
      value => value.disabled || value.checked
    );
    const allUnChecked = this.checkedList2.every(
      value => value.disabled || !value.checked
    );
    this.checkedallChecked2 = allChecked;
    this.checkedindeterminate2 = !allChecked && !allUnChecked;
  }

  //全选
  checkedcheckAll2(value) {
    if (value) {
      this.checkedList2.forEach(data => {
        if (!data.disabled) {
          data.checked = true;
        }
      });
    } else {
      this.checkedList2.forEach(data => (data.checked = false));
    }
  }

  //table加载数据时
  checkeddisplayDataChange3($event) {
    this.checkedList3 = $event;
    this.checkedrefreshStatus3();
  }

  //重置选择框
  checkedrefreshStatus3() {
    const allChecked = this.checkedList3.every(
      value => value.disabled || value.checked
    );
    const allUnChecked = this.checkedList3.every(
      value => value.disabled || !value.checked
    );
    this.checkedallChecked3 = allChecked;
    this.checkedindeterminate3 = !allChecked && !allUnChecked;
  }

  //全选
  checkedcheckAll3(value) {
    if (value) {
      this.checkedList3.forEach(data => {
        if (!data.disabled) {
          data.checked = true;
        }
      });
    } else {
      this.checkedList3.forEach(data => (data.checked = false));
    }
  }

  //table加载数据时
  checkeddisplayDataChange4($event) {
    this.checkedList4 = $event;
    this.checkedrefreshStatus4();
  }

  //重置选择框
  checkedrefreshStatus4() {
    const allChecked = this.checkedList4.every(
      value => value.disabled || value.checked
    );
    const allUnChecked = this.checkedList4.every(
      value => value.disabled || !value.checked
    );
    this.checkedallChecked4 = allChecked;
    this.checkedindeterminate4 = !allChecked && !allUnChecked;
  }

  //全选
  checkedcheckAll4(value) {
    if (value) {
      this.checkedList4.forEach(data => {
        if (!data.disabled) {
          data.checked = true;
        }
      });
    } else {
      this.checkedList4.forEach(data => (data.checked = false));
    }
  }

  //导入计划明细
  addItem(title, content, footer) {
    this.currentModal = this.modalService.open({
      title: title,
      content: content,
      footer: footer,
      maskClosable: false,
      width: 500,
      zIndex: 9999,
      style: {
        top: "40px"
      },
      onOk() {
        console.log("Click ok");
      }
    });
  }

  //添加后返回的结果
  addOutPut(event) {
    // this.isConfirmLoading = true;
    setTimeout(() => {
      /* destroy方法可以传入onOk或者onCancel。默认是onCancel */
      this.currentModal.destroy();
      // this.isConfirmLoading = false;
      this.currentModal = null;
    }, 500);
  }

  uploadChage(info: { file: UploadFile }) {
    if (info.file.status === "uploading") {
      return;
    }
    if (info.file.status === "done") {
      //设置为接口的查询不然反显有时候没用
      if (info.file.response.status != "200") {
        this._notification.create(
          "error",
          "错误提示",
          info.file.response.message
        );
      } else {
        this._notification.create("success", "提示", "导入成功");
        let params3 = { itemType: "4", adDictId: this.planDictId };
        this.httpServcie.queryCheckItem(params3).subscribe(data => {
          if (data.status == 200) {
            this.selfList = data.data.list;
            this.selfList.forEach(element => {
              this.selected.forEach(element1 => {
                if (element1.id == element.id) {
                  element.checked = true;
                }
              });
            });
          }
        });
        this.currentModal.destroy();
      }
    }
  }

  deleteItem() {
    const updateList: any = [];
    this.selfList.forEach(element => {
      if (element.checked) {
        updateList.push(element);
        this.deleteData = Object.assign({}, element);
      }
    });
    if (updateList.length > 1) {
      // 异常
      this._notification.create("warning", "警告提示", "只能选中一条数据！");
      return;
    }
    if (updateList.length == 0) {
      // 异常
      this._notification.create("warning", "警告提示", "请选中一条数据！");
      return;
    }

    let thiss = this;
    this.modalService.confirm({
      title: "您是否确认要删除这项内容",
      content: "<b>一旦删除，无法还原</b>",
      zIndex: 9999,
      onOk() {
        let ob = thiss.httpServcie.deleteCheckItem({
          id: thiss.deleteData.id
        });
        ob.subscribe(
          data => {
            if (data.status == 200) {
              thiss._notification.create("success", "成功", "删除成功");
              let params3 = { itemType: "4", adDictId: thiss.planDictId };
              thiss.httpServcie.queryCheckItem(params3).subscribe(data => {
                if (data.status == 200) {
                  thiss.selfList = data.data.list;
                  thiss.selfList.forEach(element => {
                    thiss.selected.forEach(element1 => {
                      if (element1.id == element.id) {
                        element.checked = true;
                      }
                    });
                  });
                }
              });
            } else if (data.status == 401) {
              thiss._notification.create("error", "失败", "请重新登录");
              //thiss.router.navigateByUrl("/login");
            } else {
              thiss._notification.create("error", "失败", "查询出错！请重试");
            }
          },
          error => {
            this._notification.create("error", "错误提示", "删除失败！");
          }
        );
      },
      onCancel() { }
    });
  }

  cancel($event) {
    this.outPut.emit($event);
  }
}
